package pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes;

import pl.umk.mat.fred.unicore.urc.storagemanagement.unicore.Storage;

public class BelowStorageNode extends Node {
	
	private OldPermissions permissions;
	private NewPermissions newPermissions;
	
	public static class NewPermissions {
		
		private Boolean ur;
		private Boolean uw;
		private Boolean ux;
		
		private Boolean gr;
		private Boolean gw;
		private Boolean gx;
		
		private Boolean or;
		private Boolean ow;
		private Boolean ox;
		
		public NewPermissions(Boolean ur, Boolean uw, Boolean ux,
				Boolean gr, Boolean gw, Boolean gx,
				Boolean or, Boolean ow, Boolean ox) {
			
			this.ur = ur; this.uw = uw; this.ux = ux;
			this.gr = gr; this.gw = gw; this.gx = gx;
			this.or = or; this.ow = ow; this.ox = ox;
		}

		@Override
    public boolean equals(Object obj) {
	    if (this == obj)
		    return true;
	    if (obj == null)
		    return false;
	    if (getClass() != obj.getClass())
		    return false;
	    NewPermissions other = (NewPermissions) obj;
	    if (gr == null) {
		    if (other.gr != null)
			    return false;
	    } else if (!gr.equals(other.gr))
		    return false;
	    if (gw == null) {
		    if (other.gw != null)
			    return false;
	    } else if (!gw.equals(other.gw))
		    return false;
	    if (gx == null) {
		    if (other.gx != null)
			    return false;
	    } else if (!gx.equals(other.gx))
		    return false;
	    if (or == null) {
		    if (other.or != null)
			    return false;
	    } else if (!or.equals(other.or))
		    return false;
	    if (ow == null) {
		    if (other.ow != null)
			    return false;
	    } else if (!ow.equals(other.ow))
		    return false;
	    if (ox == null) {
		    if (other.ox != null)
			    return false;
	    } else if (!ox.equals(other.ox))
		    return false;
	    if (ur == null) {
		    if (other.ur != null)
			    return false;
	    } else if (!ur.equals(other.ur))
		    return false;
	    if (uw == null) {
		    if (other.uw != null)
			    return false;
	    } else if (!uw.equals(other.uw))
		    return false;
	    if (ux == null) {
		    if (other.ux != null)
			    return false;
	    } else if (!ux.equals(other.ux))
		    return false;
	    return true;
    }

		public Boolean getUr() {
    	return ur;
    }

		public void setUr(Boolean ur) {
    	this.ur = ur;
    }

		public Boolean getUw() {
    	return uw;
    }

		public void setUw(Boolean uw) {
    	this.uw = uw;
    }

		public Boolean getUx() {
    	return ux;
    }

		public void setUx(Boolean ux) {
    	this.ux = ux;
    }

		public Boolean getGr() {
    	return gr;
    }

		public void setGr(Boolean gr) {
    	this.gr = gr;
    }

		public Boolean getGw() {
    	return gw;
    }

		public void setGw(Boolean gw) {
    	this.gw = gw;
    }

		public Boolean getGx() {
    	return gx;
    }

		public void setGx(Boolean gx) {
    	this.gx = gx;
    }

		public Boolean getOr() {
    	return or;
    }

		public void setOr(Boolean or) {
    	this.or = or;
    }

		public Boolean getOw() {
    	return ow;
    }

		public void setOw(Boolean ow) {
    	this.ow = ow;
    }

		public Boolean getOx() {
    	return ox;
    }

		public void setOx(Boolean ox) {
    	this.ox = ox;
    }
				
	}
	
	public static class OldPermissions {
		
		private Boolean r;
		private Boolean w;
		private Boolean x;
		
		public OldPermissions(Boolean r, Boolean w, Boolean x) {
	    this.r = r;
	    this.w = w;
	    this.x = x;
    }

		public Boolean getR() {
    	return r;
    }

		public void setR(Boolean r) {
    	this.r = r;
    }

		public Boolean getW() {
    	return w;
    }

		public void setW(Boolean w) {
    	this.w = w;
    }

		public Boolean getX() {
    	return x;
    }

		public void setX(Boolean x) {
    	this.x = x;
    }		
		
	}
	
	
	public Storage getStorage() {
		return getStorageNode().getStorage();
	}
	
	public StorageNode getStorageNode() {
		Node p = getParent();
		while (!(p instanceof StorageNode)) {
			p = p.getParent();
		}
		return ((StorageNode) p);
	}
	
	public String getRealName() {
		if (this instanceof MultiplyCatalogNode) {
			return ((MultiplyCatalogNode)this).getCatalogs().get(0).getRealName();
		}
		return getPath().substring(getPath().lastIndexOf("/") + 1);
	}
	
	public OldPermissions getPermissions() {
		return permissions;
	}
	
	public NewPermissions getNewPermissions() {
		return newPermissions;
	}
	
	public void setPermissions(OldPermissions permissions) {
		this.permissions = permissions;
	}
	
	public void setPermissions(String permissions) {
		if (permissions == null || permissions.length() != 9) {
			return;
		}
		newPermissions = new NewPermissions(
				permissions.charAt(0) != '-', permissions.charAt(1) != '-', permissions.charAt(2) != '-',
				permissions.charAt(3) != '-', permissions.charAt(4) != '-', permissions.charAt(5) != '-',
				permissions.charAt(6) != '-', permissions.charAt(7) != '-', permissions.charAt(8) != '-');	
	}
	
}
